package Eyjafjalla.character;

import Eyjafjalla.card.attack.Strike_Eyjafjalla;
import Eyjafjalla.card.skill.BurnGround_Eyjafjalla;
import Eyjafjalla.helper.StringHelper;
import Eyjafjalla.modcore.ClassEnum;
import Eyjafjalla.modcore.ColorEnum;
import Eyjafjalla.screens.SkinSelectScreen;
import basemod.BaseMod;
import basemod.abstracts.CustomPlayer;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.MathUtils;
import com.esotericsoftware.spine.*;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.DamageInfo;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.EnergyManager;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.cutscenes.CutscenePanel;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.helpers.FontHelper;
import com.megacrit.cardcrawl.helpers.ImageMaster;
import com.megacrit.cardcrawl.localization.CharacterStrings;
import com.megacrit.cardcrawl.screens.CharSelectInfo;
import com.megacrit.cardcrawl.screens.stats.StatsScreen;

import java.util.ArrayList;
import java.util.List;

public class Eyjafjalla extends CustomPlayer {
    public static final String ID = "eyjafjalla:Eyjafjalla";

    private static final CharacterStrings characterStrings;

    public static final String[] orbTextures = new String[]{
            "EyjafjallaResources/img/orbs/1.png",
            "EyjafjallaResources/img/orbs/2.png",
            "EyjafjallaResources/img/orbs/3.png",
            "EyjafjallaResources/img/orbs/4.png",
            "EyjafjallaResources/img/orbs/6.png",
            "EyjafjallaResources/img/orbs/5.png",
            "EyjafjallaResources/img/orbs/2.png",
            "EyjafjallaResources/img/orbs/3.png",
            "EyjafjallaResources/img/orbs/4.png",
            "EyjafjallaResources/img/orbs/6.png",
            "EyjafjallaResources/img/orbs/5.png",
    };

    public static final String VFX = "EyjafjallaResources/img/orbs/vfx.png";
    public static final String IMG_SHOULDER = "EyjafjallaResources/img/char/char_shoulder.png";
    public static final float[] LAYER_SPEED = new float[] {-32.0F, -16.0F, 64.0F, 32.0F, 64.0F, -64.0F, -64.0F, 64.0F, -30.0F, 30.0F};

    private static final int ENERGY_PER_TURN =3;
    private static final int STARTING_HP = 75;
    private static final int MAX_HP = 75;
    private static final int STARTING_GOLD = 99;
    private static final int HAND_SIZE = 0;
    private static final int ASCENSION_MAX_HP_LOSS = 7;

    private String idleName = "Skill_1_Idle";
    private String attackName = "Skill_1_Loop";

    public Eyjafjalla(String name){
        super(name, ClassEnum.Eyjafjalla_CLASS,orbTextures,VFX,LAYER_SPEED,null,null);
        this.dialogX = this.drawX+ 0.0F* Settings.scale;
        this.dialogY = this.drawY+ 240.0F*Settings.scale;
        this.initializeClass(null,IMG_SHOULDER,IMG_SHOULDER,null,this.getLoadout(),0,-5.0F,260.0F,240.0F,new EnergyManager(ENERGY_PER_TURN));
        this.refreshSkin();
    }

    public void refreshSkin(){
        int type = SkinSelectScreen.getSkin().index;
        String pathName = "EyjafjallaResources/img/char/";
        String fileName = "";
        if(type==0){
            fileName = "yb/char_1016_agoat2";
        }
        else if(type==1){
            fileName = "ybcake/char_1016_agoat2_epoque_34";
        }
        else if(type==2){
            fileName = "ss/char_180_amgoat";
        }
        else if(type==3){
            fileName = "sssummer/char_180_amgoat_summer_5";
        }
        this.atlas = new TextureAtlas(Gdx.files.internal(pathName+fileName+".atlas"));
        SkeletonJson json = new SkeletonJson(this.atlas);
        json.setScale(Settings.renderScale / 1.75F);
        SkeletonData skeletonData = json.readSkeletonData(Gdx.files.internal(pathName+fileName+".json"));
        this.skeleton = new Skeleton(skeletonData);
        this.skeleton.setColor(Color.WHITE);
        this.stateData = new AnimationStateData(skeletonData);
        this.state = new AnimationState(this.stateData);

        if(Settings.FAST_MODE){
            state.setTimeScale(1);
        }
        else{
            state.setTimeScale(1);
        }

        if(type==1){
            idleName="Skill_1_Idle";
            attackName="Skill_1_Loop";
        }
        else if (type==2){
            idleName = "Idle";
            attackName = "Attack";
        }
        else if(type==3){
            idleName = "Idle";
            attackName = "Attack";
        }
        this.state.setAnimation(0,idleName,true);
    }

    public static void onAttack(){
        AbstractPlayer pl = AbstractDungeon.player;
        if(pl.chosenClass==ClassEnum.Eyjafjalla_CLASS){
            ((Eyjafjalla)pl).attackAnimation();
        }
    }

    public void attackAnimation(){
        if(Settings.FAST_MODE){
            state.setTimeScale(1);
        }
        else{
            state.setTimeScale(1);
        }
        this.state.setAnimation(0,attackName,false);
        this.state.addAnimation(0,idleName,true,0F);
    }

    @Override
    public ArrayList<String> getStartingDeck() {
        return StringHelper.startDeck;
    }

    @Override
    public ArrayList<String> getStartingRelics() {
        return StringHelper.startRelic;
    }

    @Override
    public CharSelectInfo getLoadout() {
        return new CharSelectInfo(characterStrings.NAMES[0],characterStrings.TEXT[0],STARTING_HP,MAX_HP,HAND_SIZE,STARTING_GOLD,5,this,getStartingRelics(),getStartingDeck(),false);
    }

    @Override
    public String getTitle(PlayerClass playerClass) {
        return characterStrings.NAMES[0];
    }

    @Override
    public AbstractCard.CardColor getCardColor() {
        return ColorEnum.Eyjafjalla_COLOR;
    }

    @Override
    public Color getCardRenderColor() {
        return Color.RED.cpy();
    }

    @Override
    public AbstractCard getStartCardForEvent() {
        return new BurnGround_Eyjafjalla();
    }

    @Override
    public Color getCardTrailColor() {
        return Color.SKY.cpy();
    }

    @Override
    public int getAscensionMaxHPLoss() {
        return ASCENSION_MAX_HP_LOSS;
    }

    @Override
    public BitmapFont getEnergyNumFont() {
        return FontHelper.energyNumFontGreen;
    }

    @Override
    public void doCharSelectScreenSelectEffect() {
        CardCrawlGame.sound.playA("RELIC_DROP_MAGICAL", MathUtils.random(-0.2F, 0.2F));
    }

    @Override
    public void renderStatScreen(SpriteBatch sb, float screenX, float screenY) {
        StatsScreen.renderHeader(sb, StringHelper.colorString(this.getLocalizedCharacterName(), "#" + this.getCardRenderColor().toString()), screenX, screenY);
        this.getCharStat().render(sb, screenX, screenY);
    }

    @Override
    public void updateOrb(int energyCount) {
        this.energyOrb.updateOrb(energyCount);
    }

    @Override
    public String getCustomModeCharacterButtonSoundKey() {
        return ID;
    }

    @Override
    public String getLocalizedCharacterName() {
        return characterStrings.NAMES[0];
    }

    @Override
    public AbstractPlayer newInstance() {
        return new Eyjafjalla(this.name);
    }

    @Override
    public String getSpireHeartText() {
        return characterStrings.TEXT[2];
    }

    @Override
    public Color getSlashAttackColor() {
        return Color.SKY.cpy();
    }

    @Override
    public List<CutscenePanel> getCutscenePanels() {
        List<CutscenePanel> panels = new ArrayList();
        panels.add(new CutscenePanel("EyjafjallaResources/img/charSelect/cg01.png", "ATTACK_HEAVY"));
        panels.add(new CutscenePanel("EyjafjallaResources/img/charSelect/cg02.png","ATTACK_DAGGER_3"));
        panels.add(new CutscenePanel("EyjafjallaResources/img/charSelect/cg03.png","RELIC_DROP_MAGICAL"));
        return panels;
    }

    @Override
    public Texture getCutsceneBg() {
        return ImageMaster.loadImage("EyjafjallaResources/img/charSelect/winbg.png");
    }

    //todo
    @Override
    public AbstractGameAction.AttackEffect[] getSpireHeartSlashEffect() {
        return new AbstractGameAction.AttackEffect[]{AbstractGameAction.AttackEffect.SLASH_HEAVY, AbstractGameAction.AttackEffect.FIRE, AbstractGameAction.AttackEffect.BLUNT_HEAVY, AbstractGameAction.AttackEffect.SLASH_HEAVY, AbstractGameAction.AttackEffect.FIRE, AbstractGameAction.AttackEffect.BLUNT_HEAVY};
    }

    @Override
    public String getVampireText() {
        return characterStrings.TEXT[1];
    }

    @Override
    public void playDeathAnimation() {
        this.state.setAnimation(0,"Die",false);
    }

    @Override
    public void damage(DamageInfo info) {
        int thisHealth = this.currentHealth;
        super.damage(info);
        int trueDamage = thisHealth - this.currentHealth;
        if (info.owner != null && info.type != DamageInfo.DamageType.THORNS && trueDamage > 0 && this.isDead) {
            this.playDeathAnimation();
        }
    }



    static {
        characterStrings = CardCrawlGame.languagePack.getCharacterString(ID);
    }
}
